package controllers;

import java.util.Date;
import java.util.List;

import com.google.gson.JsonObject;

import play.mvc.Controller;
import play.mvc.With;
import models.IdeaDeveloper;
import models.Notification;
import models.Organization;
import models.OrganizationLead;
import models.Request;

@With(Secure.class)
public class OrganizationLeadInvites extends Controller {

	/**
	 * the method renders the main page of the organization lead and returns the
	 * comapnies managed in a dropdownlist if avaialble
	 * 
	 * @author : moh tarek
	 * 
	 * @param
	 * 
	 * @output void
	 */

	public static void viewOrganizationsManaged() {
		Long loggedInUserID = Security.userid();
		String loggedInUserName = Security.connected();
		IdeaDeveloper dev = IdeaDeveloper.findById(loggedInUserID);
		OrganizationLead ol = OrganizationLead.find("ideaDeveloper =?", dev)
				.first();
		List org = ol.manages;
		render(loggedInUserID, loggedInUserName, org);

	}

	/**
	 * invite an idea-dev to an organization managed by this organization lead
	 * 
	 * @author : moh tarek
	 * 
	 * @param organization
	 *            the id of the organization to be invited to
	 * 
	 * @param body
	 *            : the body of the invitation message to the idea-dev
	 * 
	 * @param dev
	 *            : the id of the OL who invited the idea-dev to join an
	 *            organization he manages
	 * 
	 * @output void
	 */
	public static void inviteToJoinOrg(String orgname, String devusername) {
		String loggedInUserName = Security.connected();
		IdeaDeveloper devol = IdeaDeveloper
				.find("username=?", loggedInUserName).first();
		OrganizationLead Organization_Lead = OrganizationLead.find(
				"ideaDeveloper=?", devol).first();
		Organization org = Organization.find("name=?", orgname).first();
		IdeaDeveloper idea_Devloper = IdeaDeveloper.find("username=?",
				devusername).first();

		Request joinOrgReq = Request.RequestMe(org, idea_Devloper,
				"JoinOrgFromOrgToDev");
		JsonObject Request_Reply = new JsonObject();
		Request_Reply.addProperty("res", "Request sent successfully!");
		renderJSON(Request_Reply.toString());
		Notification.notify(" has invited you to join the organization <a href='/application/organizationview?name="
                        + org.name
                        + "'>"
                        + org.name
                        + "</a>", idea_Devloper, devol, new Date());
	}

}
